package com.huilian.iotbox.mqtt.server.base.handler.penetrate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huilian.iotbox.data.common.RedisHelper;
import com.huilian.iotbox.data.common.RedisKey;
import com.huilian.iotbox.data.common.RedisLockKey;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.dto.DisplayRackDto;
import com.huilian.iotbox.data.enums.*;
import com.huilian.iotbox.data.po.*;
import com.huilian.iotbox.data.service.DouYinLiFeCommonService;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.utils.IotBoxUtil;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.data.vo.DisplayRackItemVo;
import com.huilian.iotbox.data.vo.FaultParamVo;
import com.huilian.iotbox.data.vo.ProductFuncVo;
import com.huilian.iotbox.mqtt.server.base.service.*;
import com.huilian.tool.weixin.service.WXSendMessageService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 策略抽象类
 * 放公用方法
 *
 * @author chenqiwen
 * @CreateDate: 2022-05-20-11-55
 * @Description:
 */
@Slf4j
public abstract class AbstractStrategyServiceImpl implements AbstractStrategyService {

    @Autowired
    private Map<String, PenetrateHandlerStrategy> penetrateHandlerStrategyMap;


    /**
     * 按串口数据包的cmd调用指定的service
     *
     * @param data
     * @param deviceNo
     * @param deviceVo
     * @param positionId
     * @throws Exception
     */
    @Override
    public void processingSerialPortData(String data, String deviceNo, DeviceVo deviceVo, Integer positionId) throws Exception {
        final String cmd = data.substring(6, 8);
        PenetrateHandlerStrategy penetrateHandlerStrategy = null;
        if ("C9".equalsIgnoreCase(cmd)) {
            final String subCmd = data.substring(8, 10);
            // cmd + sub_cmd 的组合
            String key = cmd + subCmd;
            log.info("售货机机key:{}", key);
            penetrateHandlerStrategy = penetrateHandlerStrategyMap.get("penetrateHandlerService" + key);
        } else {
            // 进入自定义
            penetrateHandlerStrategy = penetrateHandlerStrategyMap.get("penetrateHandlerService" + cmd);
        }

        if (!StringUtils.isEmpty(penetrateHandlerStrategy)) {
            log.info("接收到透传消息: {}", data);
            //执行策略
            penetrateHandlerStrategy.process(data, deviceNo, positionId);
            return;
        }
        log.info("进入自定义协议");
    }
}
